package com.api.user.controller;
import com.api.core.config.AuthUser;
import com.api.core.controller.Ctrl;
import com.api.core.response.Result;
import com.api.core.response.ResultGenerator;
import com.api.user.model.RechargeOrder;
import com.api.user.service.RechargeOrderService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.api.core.annotation.PowerEnable;
import io.swagger.annotations.*;



/**
* Created by wanghuiwen on 2021/04/08.
*/
@PowerEnable(name = "充值订单",url = "/recharge/order")
@Api(value = "充值订单", tags = {"充值订单"})
@RestController
@RequestMapping("/recharge/order")
public class RechargeOrderController extends Ctrl{
    @Resource
    private RechargeOrderService rechargeOrderService;

    @ApiOperation(value = "充值订单添加", tags = {"充值订单"}, notes = "充值订单添加")
    @PostMapping(value="/add",name="充值订单添加")
    public Result add(@RequestParam Long packageId, Authentication authentication) {
        AuthUser authUser = getAuthUser(authentication);
        return rechargeOrderService.add(packageId,authUser.getId());
    }

    @ApiOperation(value = "充值订单详细信息", tags = {"充值订单"}, notes = "充值订单详细信息")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id",required=true, value = "充值订单id", dataType = "Long", paramType = "query")
    })
    @PostMapping(value="/detail",name="充值订单详细信息")
    public Result detail(@RequestParam Integer id) {
        RechargeOrder rechargeOrder = rechargeOrderService.findById(id);
        return ResultGenerator.genSuccessResult(rechargeOrder);
    }

    @ApiOperation(value = "充值订单列表信息", tags = {"充值订单"}, notes = "充值订单列表信息")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "search", value = "查询条件json", dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "order", value = "排序json", dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "page", value = "页码", dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "size", value = "每页显示的条数", dataType = "String", paramType = "query", defaultValue = "10")
    })
    @PostMapping(value = "/list", name = "充值订单列表信息")
    public Result list(Long id,
                       Long startTime,
                       Long endTime,
                       @RequestParam(defaultValue = "{}") String order,
                       @RequestParam(defaultValue = "0") Integer page,
                       @RequestParam(defaultValue = "10") Integer size) {
        Map<String,Object> params = new HashMap<>();
        params.put("id",id);
        params.put("startTime",startTime);
        params.put("endTime",endTime);
        return rechargeOrderService.list(params, order, page, size);
    }

    @PostMapping(value = "/topup", name = "管理端充值")
    public Result topup(@RequestParam Long userId,
                        @RequestParam Integer coin) {
        return rechargeOrderService.topup(userId, coin);
    }

    @ApiOperation(value = "支付回调", tags = {"充值订单"}, notes = "支付回调")
    @PostMapping(value="/payment/callback",name="支付回调")
    public String add(@RequestParam String MerchantCode,
                      @RequestParam String PaymentId,
                      @RequestParam String RefNo,
                      @RequestParam String Amount,
                      @RequestParam String Currency,
                      @RequestParam String Remark,
                      @RequestParam String TransId,
                      @RequestParam String AuthCode,
                      @RequestParam String Status,
                      @RequestParam String ErrDesc,
                      @RequestParam String Signature,
                      @RequestParam String CCName,
                      @RequestParam String CCNo,
                      @RequestParam String S_bankname,
                      @RequestParam String S_country) {
        return rechargeOrderService.callback(MerchantCode,PaymentId,RefNo,Amount,Currency,Remark,TransId,AuthCode,Status,ErrDesc,Signature,CCName,CCNo,S_bankname,S_country);
    }
}
